import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

/**
 * @Author syx
 * @Desc
 * @Create 2020/6/13 17:23
 */
public class ImageApp extends JPanel {

    private JFrame frame;

    public ImageApp() {

    }

    private void initUI() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new ImageApp());
        frame.setPreferredSize(new Dimension(800, 600));

        // 菜单
        JMenuBar menubar = new JMenuBar();
        frame.setJMenuBar(menubar);
        JMenu fileMenu = new JMenu("文件");
        menubar.add(fileMenu);

        JMenuItem openItem = fileMenu.add("打开");
        openItem.addActionListener(openListener);

        JMenuItem exportItem = fileMenu.add("导出");
        exportItem.addActionListener(exportListener);

        JMenuItem exitItem = fileMenu.add("导出");
        exitItem.addActionListener(exitListener);

        JMenu setMenu = new JMenu("设置");
        menubar.add(setMenu);

        JMenuItem historyItem = setMenu.add("历史记录");
        historyItem.addActionListener(historyListener);

        JMenuItem saveDirItem = setMenu.add("存储目录");
        saveDirItem.addActionListener(saveDirListener);

        JMenuItem aboutItem = setMenu.add("关于");
        aboutItem.addActionListener(aboutListener);

        label = new JLabel();
        frame.add(label);
        chooser = new JFileChooser();
        chooser.setCurrentDirectory(new File("."));

        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        this.frame = frame;
    }

    public void start() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                initUI();

                showList();

            }
        });
    }

    private JLabel label;
    private JFileChooser chooser;

    // 打开 按钮监听事件
    ActionListener openListener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("openMenu...");
            System.out.println(e);

            // TODO Auto-generated method stub
            int result = chooser.showOpenDialog(null);
            if (result == JFileChooser.APPROVE_OPTION) {
                String name = chooser.getSelectedFile().getPath();
                label.setIcon(new ImageIcon(name));
            }
        }
    };
    // 导出 按钮监听事件
    ActionListener exportListener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("exportMenu...");
            System.out.println(e);
        }
    };
    // 退出 按钮监听事件
    ActionListener exitListener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("exitMenu...");
            System.out.println(e);
        }
    };
    // 历史记录 按钮监听事件
    ActionListener historyListener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("historyMenu...");
            System.out.println(e);
        }
    };
    // 历史记录 按钮监听事件
    ActionListener saveDirListener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("saveDirMenu...");
            System.out.println(e);
        }
    };
    // 关于 按钮监听事件
    ActionListener aboutListener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("aboutMenu...");
            System.out.println(e);
        }
    };

    // 图片列表展示
    private void showList() {
//        showSingleImage();
        Frame f = new Frame();
       f.setLayout(new GridLayout(3,2));    //容器平均分成3行2列
        f.add(new Button("1"));           //添加到第1行第1格
        f.add(new Button("2"));           //添加到第1行第2格
        f.add(new Button("3"));           //添加到第2行第1格
        f.add(new Button("4"));           //添加到第2行第2格
        f.add(new Button("5"));           //添加到第3行第1格
        f.add(new Button("6"));           //添加到第3行第2格

        frame.add(f);
    }

    // 单张图片显示
    private void showSingleImage() {
        JLabel imgLab = new JLabel();

        ImageIcon colorIcon = new ImageIcon("C:\\Users\\Administrator\\Desktop\\test\\1115571-20190808170234241-354168426.png");
        colorIcon.setImage(colorIcon.getImage().getScaledInstance(320, 240, Image.SCALE_DEFAULT));//宽高根据需要设定
        imgLab.setIcon(colorIcon);

        frame.add(imgLab);
    }
}
